package com.plian.system.service.wf;

import com.plian.Tools.TokenUtil;
import com.plian.system.bean.wf.UserTaskPOJO;
import com.plian.system.dao.wf.WfCompletedTaskMapper;
import com.plian.system.helper.wf.SimulationHelper;
import com.plian.system.helper.wf.WorkFlowHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 单据中对工作流操作的服务类
 */
@Slf4j
@Service
@SuppressWarnings("Duplicates")
public class FormWorkflowService {
    @Autowired(required = false)
    @Lazy
    private HistoryService historyService;

    @Autowired(required = false)
    @Lazy
    private RuntimeService runtimeService;

    @Autowired
    @Lazy
    private WorkflowService workflowService;

    @Autowired
    @Lazy
    private WorkFlowHelper workFlowHelper;

    @Autowired
    @Lazy
    private SimulationHelper simulationHelper;
    @Autowired
    @Lazy
    private SimulationHelperTwo simulationHelperTwo;

    @Autowired(required = false)
    @Lazy
    private WfCompletedTaskMapper wfCompletedTaskMapper;

    /**
     * 关联删除工作流的单据信息
     *
     * @param formId 单据id
     */
    @Transactional
    public void deleteProcessInstance(String formId) {
        List<HistoricProcessInstance> list = historyService
                .createHistoricProcessInstanceQuery()
                .processInstanceBusinessKey(formId)
                .unfinished()
                .list();
        if (list != null && list.size() == 1) {
            runtimeService.deleteProcessInstance(list.get(0).getId(), "");
            historyService.deleteHistoricProcessInstance(list.get(0).getId());
            wfCompletedTaskMapper.deleteByFormId(formId);
        }
    }

    /**
     * 单据保存时开启流程并且进行模拟
     *
     * @param formId          单据id
     * @param processVariable 流程变量
     */
    @Transactional
    public void startProcessAndSimulate(String formId, String formType, Map<String, Object> processVariable) {
        long count = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(formId).count();
        if (count > 0) {
            return;
        }
        workflowService.startProcessInstanceByKeyAndTenantId(formId, Integer.parseInt(formType), processVariable, TokenUtil.getTokenUserId());
        ProcessInstance processInstance = runtimeService
                .createProcessInstanceQuery()
                .processInstanceBusinessKey(formId)
                .singleResult();
        LinkedList<UserTaskPOJO> userTaskPOJOS = null;
        try {
            /*if(formType.equals("50104")||formType.equals("132001")||formType.equals("132002")||formType.equals("50094")){
                userTaskPOJOS= simulationHelperTwo.simulation(processInstance, processVariable, false);
            }else{*/
            userTaskPOJOS = simulationHelper.simulation(processInstance, processVariable, false);
            /*}*/

        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
        }
        if (userTaskPOJOS==null) {
            return;
        }
        workFlowHelper.saveUserTaskListIntoVariable(userTaskPOJOS, processInstance.getId());
    }
}
